<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/10
 * Time: 10:13
 */

namespace app\admin\controller;


use app\admin\model\Admin;
use app\admin\model\AuthGroup;
use app\admin\model\AuthGroupAccess;
use think\Db;
use think\Session;

class AdminController extends BaseController
{
    public function index()
    {
        $this->assign([
            'list' => Admin::with('authGroup')->select(),

        ]);
        return $this->fetch();
    }

    public function add()
    {
        if($this->request->isPost()){
            $data = input('post.');
            $result = $this->validate($data,'Admin.add');
            if(true !== $result){
                // 验证失败 输出错误信息
                return array('status' => 0,'msg' => $result,'icon'=> 2 );
            }
            Db::startTrans();
            try{
                $Admin = new Admin;
                $Admin->user_name   = $data['user_name'];
                $Admin->password    = MD5($data['password']);
                $Admin->email       = $data['email'];
                $Admin->save();
                $getLastID = $Admin->id;
                if($data['group_id'] == 1){
                    return array('status' => 0,'msg' => '不能添加超级管理员','icon' => 2);
                }
                AuthGroupAccess::create(['uid' => $getLastID,'group_id' => $data['group_id']]);
                // 提交事务
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
            }
            if(isset($e)){
                return array('status' => 0,'msg' => '服务器内部错误,请联系管理员','icon' => 2);
            }else{
                return array('status' => 1,'msg' => '添加管理员成功','icon' => 1);
            }
        }
        $this->assign([
            'AuthGroup' => AuthGroup::getGroup(),
        ]);
        return $this->fetch();

    }

    public function edi()
    {
        if($this->request->isPost()) {
            $data = input('post.');
            if(!$data['password']){
                unset($data['password']);
                unset($data['repassword']);
                $result = $this->validate($data,'Admin.edi');
            }else{
                $result = $this->validate($data,'Admin.edi2');
                $data['password'] = md5($data['password']);
            }
            if(true !== $result){
                // 验证失败 输出错误信息
                return array('status' => 0,'msg' => $result,'icon' => 2);
            }

            Db::startTrans();
            try{
                $Admin = new Admin();
                $Admin->allowField(true)->save($data,['id' => $data['id']]);
                if(isset($data['group_id'])){
                    if($data['group_id'] == 1){
                        return array('status' => 0,'msg' => '不能修改成超级管理员');
                    }else{
                        AuthGroupAccess::where('uid',$data['id'])->update(['group_id' => $data['group_id']]);
                    }
                }
                //提交事务
                Db::commit();
            } catch (\Exception $e) {
                //回滚事务
                Db::rollback();
            }
            if(!isset($e)){
                return array('status' => 1,'msg' => '修改管理员成功','icon' => 1);
            }else{
                return array('status' => 0,'msg' => '修改管理员失败','icon' => 2);
            }

        }
        $this->assign([
            'AuthGroup' => AuthGroup::getGroup(),
            'getOneID' => Admin::with('authGroup')->where('id',input('id/d'))->find(),
        ]);
        return $this->fetch();
    }

    public function del()
    {
        $id = input('id/d');
        $info = Admin::with('authGroup')->find($id);
        if($info['auth_group'][0]['id'] == 1){
            return array('status' => 0,'msg' => '不能删除超级管理员','icon' => 2);
        }
        Db::startTrans();
        try{
            Admin::destroy($id);
            AuthGroupAccess::where('uid','eq',$id)->delete();
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
        if(!isset($e)){
            return array('status' => 1,'msg' => '删除成功','icon' => 1);
        }else{
            return array('status' => 0,'msg' => '服务器内部错误,请联系管理员','icon' => 2);
        }
    }

    public function change()
    {
        $id = input('id/d');
        $info = Admin::with('authGroup')->find($id);
        if($info['auth_group'][0]['id'] == 1){
            return array('status' => 0,'msg' => '不能停用超级管理员','icon' => 2);
        }
        if($info['state'] == 1){
            $result = $info->save(['state' => 0]);
            if($result){
                return array('msg' => '已禁用','status' => 1,'icon' => 1);
            }
        }else{
            $result = $info->save(['state' => 1]);
            if($result){
                return array('msg' => '已启用','status' => 1,'icon' => 1);
            }
        }
    }

    public function password_edi()
    {
        if($this->request->isPost()){
            $data = input('post.');
            $result = $this->validate($data,'Admin.password_edi');
            if(true !== $result){
                // 验证失败 输出错误信息
                return array('status' => 0,'msg' => $result,'icon' => 2);
            }
            $user_name = Session::get('admin_name');
            $password = md5($data['opassword']);
            $result = Admin::where(['user_name' => $user_name,'password' => $password])->find();
            if(!$result){
                return array('status' => 0,'msg' => '原密码不正确','icon' => 2);
            }
            $password = md5($data['password']);
            $Admin = new Admin;
            $Admin->isupdate(true)->update(['password' => $password],['user_name' => $user_name]);
            return array('status' => 1,'msg' => '密码成功修改','icon' => 1);
        }
        return $this->fetch();
    }
}